<template>
  <div class="business-user-permission">
    <div v-for="(item,index) in Permissions" :key="index" class="business-user-permission-page">
      <div class="business-user-permission-page-title">
        {{ item.TableAlias }}
      </div>
      <el-checkbox-group v-model="UserPermissions">
        <el-checkbox v-for="(btn,BIndex) in item.BUTTONS" :key="BIndex" :label="item.TableName+'::'+btn.key">
          {{ btn.name }}
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <el-button style="margin: 10px;float: right" size="mini" type="primary" @click="SaveUserPermission">
      保存权限
    </el-button>
  </div>
</template>
<script>
import SqlTool from '../../utils/SqlTool'
import Utils from '../../utils/Common'
import { MessageSuccess, MessageWarning } from "@/utils";
import { DataManage } from "@/api/DataManageApi";

export default {
  name: 'UserPermissionsDialog',
  data () {
    return {
      TableNames: [],
      ConfigUser: {},
      UserPermissions: [],
      Permissions: []
    }
  },
  methods: {
    /**
     * 加载用户权限
     * @constructor
     */
    LoadUserPermissions (User, _TableNames) {
      this.UserPermissions = []
      const UserInfo = Utils.GetLoginUserInfo(_TableNames)
      this.TableNames = _TableNames
      this.ConfigUser = User
      this.Permissions = UserInfo.Permissions || []
      const Pages = this.Permissions.map(p => p.page)
      for (const tableName of _TableNames) {
        if (!Pages.includes(tableName.TableName)) {
          continue
        }
        const BUTTONS = []
        for (const permissionsKey of UserInfo.Permissions) {
          if (permissionsKey.page === tableName.TableName) {
            const name = SqlTool.TABLES.BUTTONS.filter(b => b.key === permissionsKey.key)[0]
            BUTTONS.push({ key: permissionsKey.key, name: name.name })
          }
        }
        tableName.BUTTONS = BUTTONS
        this.Permissions.push(tableName)
      }
      const TableName = SqlTool.TABLES.SYS_USER_PERMISSION
      const Sql = SqlTool.GeneratedQuerySql(TableName, null, null, 'user_id=' + User.id)
      DataManage.QueryBySql(Sql).then(res => {
        const UserPermissions = []
        for (const tdElement of res.Data.Data) {
          UserPermissions.push(tdElement.page + '::' + tdElement.key)
        }
        this.UserPermissions = UserPermissions
      })
    },
    /**
     * 保存用户权限
     * @constructor
     */
    SaveUserPermission () {
      const ut = SqlTool.TABLES.SYS_USER_PERMISSION
      const UserInfo = this.ConfigUser
      const DeleteSql = SqlTool.GeneratedDeleteSql({ name: ut }, 'user_id=' + UserInfo.id)
      let Sql = DeleteSql + ';' + SqlTool.INSERT_SQL + ut + ' (page, key,user_id ) values '
      const Data = []
      for (const userPermission of this.UserPermissions) {
        const up = userPermission.split('::')
        Data.push('(' + [`'${up[0]}'`, `'${up[1]}'`, `'${UserInfo.id}'`].join(',') + ')')
      }
      Sql += Data.join(',') + ';'
      DataManage.ExecuteSql(Sql).then(res => {
        res.IsSuccess ? MessageSuccess('保存成功，受影响行数' + res.Data) : MessageWarning('保存失败，受影响行数' + res.Data)
      })
    }
  },
  mounted () {
  }
}
</script>

<style scoped lang="less">

.business-user-permission {
  padding: 10px;
  margin-bottom: 30px;

  .business-user-permission-page {
    padding: 10px 0;
    border-bottom: 1px solid #e7e6e6;

    .business-user-permission-page-title {
      padding-bottom: 10px;
    }
  }
}
</style>
